$(document).ready(function() {
	showLogin();
	$('#logoU').addClass('animated bounceIn');
	
	
	$("#login-btn").click(controlLogin);
	
	$("#form-login").submit(function(event) {
		
		
		event.preventDefault();
		currentForm = this;
		
		if(exists()) {
			currentForm.submit();
		}
		else {
			myAlert("ACCOUNT NON ESISTENTE","error","topCenter");
			
			$('#login-btn').addClass('animated shake');
			$('#login-btn').one('webkitAnimationEnd mozAnimationEnd oAnimationEnd animationEnd', 
					function(){ 
//				$("#login-btn").removeClass("animated hinge");  
//			               $('#login-btn').addClass('animated fadeInUpBig');
			               
			               $('#usr').val("");
			               $('#psw').val("");
			               $('#login-btn').removeClass('animated shake');
			               
				                });

			
		}
		
	});

});

//function enterFromLeft()


function exists() {
	
var esiste = true;
	
	$.ajax({
		type: "POST",
		async: false,
		url: "./IsAnAccountServlet",
		data: {"usr" :$('#usr').val() , "psw":$('#psw').val() },
		dataType: "xml",

		success: function(data) {
			
			$(data).find('report').each(function() {
				
				var account = $(this).find('account').text();
				if(account == "0") esiste = false;
				
				else esiste = true;
			
				
			}); // FINE EACH FUNCTION

		}
	});
	
	return esiste;
}

function controlLogin() {
	var lunghezzaUsr = $('#usr').val().length;
	
	if (lunghezzaUsr<6 || lunghezzaUsr>20){
		myAlert("Inserire un Username Compresa tra 6 e 15 caratteri","warning","centerRight");
	
		return false;
	}
		
	
	var lunghezzaPsw = $('#psw').val().length;
	
	if (lunghezzaPsw<6 || lunghezzaPsw>15){
		myAlert("Inserire una Password Compresa tra 6 e 15 caratteri","warning","centerRight");
		
		return false;
	}
		return true;
}

function myAlert(x,y,z) {
	
	var n = noty({text: (""+x ), layout : ""+z , type : ""+y, timeout: 1500 });
}



function showSearch() {
	$(".form-signin").addClass("hidden");
	$(".search-result-panel").addClass("hidden");
	$(".search").removeClass("hidden");
	$(".show-product").addClass("hidden");
	$("li#home-btn").removeClass("active");
	$("li#search-btn").addClass("active");
}

function showLogin() {
	$(".form-signin").removeClass("hidden");
	$(".search").addClass("hidden");
	$(".search-result-panel").addClass("hidden");
	$(".show-product").addClass("hidden");
	$("li#home-btn").addClass("active");
	$("li#search-btn").removeClass("active");
}

function showSearchResult() {
	$(".form-signin").addClass("hidden");
	$(".search").addClass("hidden");
	$(".search-result-panel").removeClass("hidden");
	$(".show-product").addClass("hidden");
	$("li#home-btn").removeClass("active");
	$("li#search-btn").addClass("active");
}

function showProduct() {
	$(".form-signin").addClass("hidden");
	$(".search").addClass("hidden");
	$(".search-result-panel").addClass("hidden");
	$(".show-product").removeClass("hidden");
	$("li#home-btn").removeClass("active");
	$("li#search-btn").addClass("active");
}

